home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 (Alt) / MACD 5.bin / workbench / libs / unixlib.lha / unix / src / zoneinfo / ialloc.c next >
Encoding:
C/C++ Source or Header  |  1996-10-24  |  3.7 KB  |  133 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Arthur David Olson of the National Cancer Institute.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)ialloc.c    5.3 (Berkeley) 4/20/91";
  39. #endif /* not lint */
  40.  
  41. #ifdef notdef
  42. static char    elsieid[] = "@(#)ialloc.c    8.18";
  43. #endif
  44.  
  45. /*LINTLIBRARY*/
  46.  
  47. #include <sys/cdefs.h>
  48. #include <sys/types.h>
  49. #include <string.h>
  50. #include <stdlib.h>
  51.  
  52. #ifdef MAL
  53. #define NULLMAL(x)    ((x) == NULL || (x) == MAL)
  54. #else /* !defined MAL */
  55. #define NULLMAL(x)    ((x) == NULL)
  56. #endif /* !defined MAL */
  57.  
  58. #define nonzero(n)    (((n) == 0) ? 1 : (n))
  59.  
  60. char *    icalloc __P((int nelem, int elsize));
  61. char *    icatalloc __P((char * old, const char * new));
  62. char *    icpyalloc __P((const char * string));
  63. char *    imalloc __P((int n));
  64. char *    irealloc __P((char * pointer, int size));
  65. void    ifree __P((char * pointer));
  66.  
  67. char *
  68. imalloc(int n)
  69. {
  70. #ifdef MAL
  71.     register char *    result;
  72.  
  73.     result = malloc((size_t) nonzero(n));
  74.     return NULLMAL(result) ? NULL : result;
  75. #else /* !defined MAL */
  76.     return malloc((size_t) nonzero(n));
  77. #endif /* !defined MAL */
  78. }
  79.  
  80. char *
  81. icalloc(int nelem, int elsize)
  82. {
  83.     if (nelem == 0 || elsize == 0)
  84.         nelem = elsize = 1;
  85.     return calloc((size_t) nelem, (size_t) elsize);
  86. }
  87.  
  88. char *
  89. irealloc(char *pointer, int size)
  90. {
  91.     if (NULLMAL(pointer))
  92.         return imalloc(size);
  93.     return realloc((void *) pointer, (size_t) nonzero(size));
  94. }
  95.  
  96. char *
  97. icatalloc(char *old, const char *new)
  98. {
  99.     register char *    result;
  100.     register    oldsize, newsize;
  101.  
  102.     newsize = NULLMAL(new) ? 0 : strlen(new);
  103.     if (NULLMAL(old))
  104.         oldsize = 0;
  105.     else if (newsize == 0)
  106.         return old;
  107.     else    oldsize = strlen(old);
  108.     if ((result = irealloc(old, oldsize + newsize + 1)) != NULL)
  109.         if (!NULLMAL(new))
  110.             (void) strcpy(result + oldsize, new);
  111.     return result;
  112. }
  113.  
  114. char *
  115. icpyalloc(const char *string)
  116. {
  117.     return icatalloc((char *) NULL, string);
  118. }
  119.  
  120. void
  121. ifree(char *p)
  122. {
  123.     if (!NULLMAL(p))
  124.         (void) free(p);
  125. }
  126.  
  127. void
  128. icfree(char * const p)
  129. {
  130.     if (!NULLMAL(p))
  131.         (void) free(p);
  132. }
  133.